package com.twl.ws.web;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.twl.app.base.bo.CryptService;
import com.twl.app.base.bo.batchsave.BatchSaveService;
import com.twl.app.base.bo.salarysumreport.SalarySumReportService;
import com.twl.app.base.ctrl.CommonCtrlImpl;
import com.twl.app.base.ctrl.SalaryImportImp;
import com.twl.app.base.entity.CheckDate;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Companyb;
import com.twl.app.base.entity.Miniwase;
import com.twl.app.base.entity.OpenState;
import com.twl.app.base.entity.SalParam;
import com.twl.app.base.entity.SalSalaryData;
import com.twl.app.base.entity.SalSocialAdjust;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.DateComm;
import com.twl.app.base.util.GsonUtils;
import com.twl.app.base.util.WsPageUtils;
import com.twl.app.base.vo.Umplistvo;
import com.twl.core.framework.page.ExpResult;
import com.twl.core.framework.page.Result;
import com.twl.facade.web.SalaryImport;

@Service("salaryImportimp")
public class SalaryImportimp implements SalaryImport {
	@Autowired
	private SalaryImportImp salaryImportImp;
	@Autowired
	private CommonCtrlImpl commonCtrlImpl;
	@Autowired
	private DateComm dateComm ;
	
	@Autowired
	private BatchSaveService batchSaveService;
	@Autowired
	private SalarySumReportService  salarySumReportService;

	/***
	 * 考勤
	 */
	@Override
	public void saveimportchecktemplet(List<Checktemplet> m) {

		for (Checktemplet checktemplet : m) {
			salaryImportImp.savechecktemplet(checktemplet);
		}

		if (m != null) {
			OpenState os = commonCtrlImpl.getOpenState(dateComm.getNextYearMonth(m.get(0).getPeriod()));
			if (os != null) {
				os.setCounting(os.getCounting() + 1);
				os.setOpenCheck("0");
				String result = commonCtrlImpl.saveOrUpdateOpenState(os);
			} else {
				OpenState osNew = new OpenState();
				osNew.setOpenDate(dateComm.getNextYearMonth(m.get(0).getPeriod()));
				osNew.setCounting(1);
				osNew.setOpenCheck("0");
				String result = commonCtrlImpl.saveOrUpdateOpenState(osNew);
			}
		}

	}

	/***
	 * 增加员工
	 */
	@Override
	public void saveimportuemp(List<Uemp> ump)  {
		
		
		for (int i = 0; i < ump.size(); i++) {
			Uemp uemp = new Uemp();
			try {
				BeanUtils.copyProperties(uemp, ump.get(i));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			String em = null;
			String result = null;
			em = salaryImportImp.saveump(ump.get(i));
			result = commonCtrlImpl.insertJB(uemp);
		}
	}

	/**
	 * 增加社保
	 * 
	 * @param com
	 */
	public void saveimportcompanyb(List<Companyb> com) {
		for (int i = 0; i < com.size(); i++) {
			String nu = salaryImportImp.savecompanyb(com.get(i));
			System.out.println(nu);
		}
	}

	/***
	 * 查询员工信息web调用这个方法改了参数
	 */
	@Override
	public Umplistvo selectuemp(String id) {
		Umplistvo vo = new Umplistvo();
		Uemp uemp = null;
		if (salaryImportImp.selectuemp(id).size() > 0) {
			uemp = salaryImportImp.selectuemp(id).get(0);
		} else {
			uemp = null;
		}
		vo.setUempvo(uemp);
		return vo;
	}

	/***
	 * 查询考勤
	 */
	@Override
	public String selectchecktemplet(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		Result ult = salaryImportImp.selectchecktemplet(param);
		return GsonUtils.toJson(ult);
	}

	/***
	 * 导出考勤
	 */
	@Override
	public String expChecktemplet(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expChecktemplet(param);
		return GsonUtils.toJson(expResult);
	}

	/***
	 * 导出参保比例
	 */
	@Override
	public String expCompanyb(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expCompanyb(param);
		return GsonUtils.toJson(expResult);
	}

	/***
	 * 导出薪资数据
	 */
	@Override
	public String expSalSalaryData(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expSalSalaryData(param);
		return GsonUtils.toJson(expResult);
	}

	/***
	 * 导出员工
	 */
	@Override
	public String expUemp(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expUemp(param);
		return GsonUtils.toJson(expResult);
	}

	/**
	 * 查询社保表
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public String selectcomp(String params) {

		Map<String, Object> param = WsPageUtils.getParams(params);
		Result ult = salaryImportImp.selectcomp(param);
		return GsonUtils.toJson(ult);
	}

	/***
	 * 修改社保
	 */
	@Override
	public String updatecomp(String map) {
		Map<String, Object> param = WsPageUtils.getParams(map);
		int num = salaryImportImp.updatecomp(param);
		if (num > 0) {
			return "yes";
		} else {
			return "no";
		}
	}

	/**
	 * 保存出勤日期
	 */

	@Override
	public String savaDate(List<CheckDate> CheckDateList) {
		try {
			for (CheckDate checkDate : CheckDateList) {
				String result = salaryImportImp.insertDate(checkDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "false";

		}
		return "success";

	}

	/**
	 * 修改出勤日期
	 */
	@Override
	public String updateDate(String map) {
		Map<String, Object> param = WsPageUtils.getParams(map);
		String result = salaryImportImp.updateDate(param);

		return result;
	}

	/**
	 * 批量修改出勤日期
	 * 
	 * @param ids
	 * @return
	 */
	public Integer updateWorkdays(Integer[] ids) {
		return salaryImportImp.updateWorkdays(ids);
	}

	public Integer updateWeekdays(Integer[] ids) {
		return salaryImportImp.updateWeekdays(ids);
	}

	/***
	 * 删除员工
	 */
	@Override
	public String deluemp(String id) {
		if (salaryImportImp.deluemp(id) == 1) {
			return "yes";
		} else {
			return "no";
		}
	}

	/**
	 * 批量删除员工
	 */
	public Integer batchDelUemp(Integer[] ids) {
		return salaryImportImp.batchDelUemp(ids);
	}

	/*
	 * 删除社保
	 */
	@Override
	public String delcomp(String id) {
		if (salaryImportImp.delcomp(id) > 0) {
			return "yes";
		} else {
			return "no";
		}

	}

	/**
	 * 批量删除社保
	 * 
	 * @param ids
	 * @return
	 */
	public Integer batchDelCompanyb(Integer[] ids) {
		return salaryImportImp.batchDelCompanyb(ids);
	}

	/***
	 * 查询员工
	 */

	public String selectuempstr(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		Result ult = salaryImportImp.selectuempstr(param);
		return GsonUtils.toJson(ult);

	}

	/*
	 * 删除考勤
	 * 
	 * @see com.twl.facade.web.SalaryImport#delChecktemplet(java.lang.String)
	 */
	public void delChecktemplet(String id) {
		salaryImportImp.delChecktemplet(id);
	}

	/**
	 * 批量删除考勤记录
	 * 
	 * @param id
	 */
	public void batchDelChecktemplet(Integer[] ids) {
		salaryImportImp.batchDelChecktemplet(ids);
	}

	@Override
	public Integer saveSalSalaryData(SalSalaryData data) {
		return salaryImportImp.saveSalSalaryData(data);
	}

	@Override
	public Integer batchSaveSalSalaryData(List<SalSalaryData> list) {
		int i = 0;
		for (SalSalaryData data : list) {
			saveSalSalaryData(data);
			i++;
		}
		return i;
	}

	@Override
	public Integer delSalSalaryData(Integer[] ids) {
		return salaryImportImp.delSalSalaryData(ids);
	}

	@Override
	public String querySalaryData(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		Result result = salaryImportImp.querySalaryData(params);
		return GsonUtils.toJson(result);
	}

	/***
	 * 查询出勤日期
	 */

	public String queryDate(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		Result result = salaryImportImp.queryDate(params);
		return GsonUtils.toJson(result);

	}

	/**
	 * 导出社保调整
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public String expSalSocialAdjust(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expSalSocialAdjust(param);
		return GsonUtils.toJson(expResult);
	}

	/**
	 * 增加 修改 社保调整
	 */
	@Override
	public Integer saveSalSocialAdjust(SalSocialAdjust data) {
		return salaryImportImp.saveSalSocialAdjust(data);
	}

	/**
	 * 批量保存社保调整
	 */
	@Override
	public Integer batchSaveSalSocialAdjust(List<SalSocialAdjust> list) {
		int i = 0;
		for (SalSocialAdjust data : list) {
			saveSalSocialAdjust(data);
			i++;
		}
		return i;
	}

	/**
	 * 删除 社保调整
	 */
	@Override
	public Integer delSalSocialAdjust(Integer[] ids) {
		return salaryImportImp.delSalSocialAdjust(ids);
	}

	/**
	 * 查询 社保调整
	 */
	@Override
	public String querySalSocialAdjust(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		Result result = salaryImportImp.querySalSocialAdjust(params);
		return GsonUtils.toJson(result);
	}

	/**
	 * 导出薪资系统参数
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public String expSalParam(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expSalParam(param);
		return GsonUtils.toJson(expResult);
	}

	/**
	 * 增加 修改 薪资系统参数
	 */
	@Override
	public Integer saveSalParam(SalParam data) {
		return salaryImportImp.saveSalParam(data);
	}

	/**
	 * 批量保存薪资系统参数
	 */
	@Override
	public Integer batchSaveSalParam(List<SalParam> list) {
		int i = 0;
		for (SalParam data : list) {
			saveSalParam(data);
			i++;
		}
		return i;
	}

	/**
	 * 删除 薪资系统参数
	 */
	@Override
	public Integer delSalParam(Integer[] ids) {
		return salaryImportImp.delSalParam(ids);
	}

	/**
	 * 查询 薪资系统参数
	 */
	@Override
	public String querySalParam(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		Result result = salaryImportImp.querySalParam(params);
		return GsonUtils.toJson(result);
	}

	/**
	 * 查询 卡中心工资明细及报表数据汇总
	 */
	@Override
	public String querySalarySumReport(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		
		
		Result result = salarySumReportService.querySalarySumReport(params);
		
		
		return GsonUtils.toJson(result);
	}

	/**
	 * 导出卡中心工资明细及报表数据汇总
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public String expSalarySumReport(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salarySumReportService.expSalarySumReport(param);
		return GsonUtils.toJson(expResult);
	}

	/**
	 * 导出最低工资参数
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public String expMiniwase(String params) {
		Map<String, Object> param = WsPageUtils.getParams(params);
		ExpResult expResult = salaryImportImp.expMiniwase(param);
		return GsonUtils.toJson(expResult);
	}

	/**
	 * 增加 修改 最低工资参数
	 */
	@Override
	public Integer saveMiniwase(Miniwase data) {
		return salaryImportImp.saveMiniwase(data);
	}

	/**
	 * 批量保存最低工资参数
	 */
	@Override
	public Integer batchSaveMiniwase(List<Miniwase> list) {
		int i = 0;
		for (Miniwase data : list) {
			saveMiniwase(data);
			i++;
		}
		return i;
	}

	/**
	 * 删除 最低工资参数
	 */
	@Override
	public Integer delMiniwase(Integer[] ids) {
		return salaryImportImp.delMiniwase(ids);
	}

	/**
	 * 查询 最低工资参数
	 */
	@Override
	public String queryMiniwase(String json) {
		Map<String, Object> params = WsPageUtils.getParams(json);
		Result result = salaryImportImp.queryMiniwase(params);
		return GsonUtils.toJson(result);
	}

	@Override
	public void batchSaveImportChecktemplet(List<Checktemplet> list) {
		batchSaveService.batchSaveImportChecktemplet(list);
		
	}

	@Override
	public void batchSaveImportCompanyb(List<Companyb> list) {
		batchSaveService.batchSaveImportCompanyb(list);
		
	}

	@Override
	public void batchSaveImportMiniwase(List<Miniwase> list) {
		batchSaveService.batchSaveImportMiniwase(list);
		
	}

	@Override
	public void batchSaveImportSalParam(List<SalParam> list) {
		batchSaveService.batchSaveImportSalParam(list);
		
	}

	@Override
	public void batchSaveImportSalSalaryData(List<SalSalaryData> list) {
		batchSaveService.batchSaveImportSalSalaryData(list);
		
	}

	@Override
	public void batchSaveImportSalSocialAdjust(List<SalSocialAdjust> list) {
		batchSaveService.batchSaveImportSalSocialAdjust(list);
		
	}

	@Override
	public void batchSaveImportUemp(List<Uemp> list) {
		batchSaveService.batchSaveImportUemp(list);
		
	}

	@Override
	public List<String> getDesKeyList() {
		return CryptService.getDesKeyList();
	}

	@Override
	public String getValueByKey(String desKey) {
		return CryptService.getValueByKey(desKey);
	}

}
